
#include "MessageWindow.hpp"

#include <Wt/WText>
// #include <Wt/WContainerWidget>
// #include <Wt/WApplication>
// #include <Wt/WVBoxLayout>
// #include <Wt/WSlider>
// #include <Wt/WImage>
// #include <Wt/WTextArea>
// #include <Wt/WText>
// #include <Wt/WScrollArea>
// #include "ControlButton.hpp"
// 
// #include <iostream>
// 
// #include <Wt/WCssStyleSheet>
// 
// 
// #include "CBStyle.hpp" 



class LeftTopBox : public Wt::WBoxLayout
{

public:

    LeftTopBox()
        :
            Wt::WBoxLayout(Wt::WBoxLayout::Direction::BottomToTop)
    {
        Wt::WText* message_window = new Wt::WText;
        Wt::WApplication::instance()->styleSheet().addRule(".messageviewer_titlebar",
                                                        "color: white;"
                                                        "font-size:larger;"
                                                        "border: thin ridge black;"
                                                        "background: linear-gradient(to bottom, silver, gray, dimgray);");
        
        Wt::WText* titlebar = new Wt::WText("&nbsp;CARBLogger_UI v.0", Wt::TextFormat::XHTMLText);
        titlebar->setStyleClass("messageviewer_titlebar");
        //titlebar->resize(Wt::WLength::Auto, "1.3em"); //this is not needed is it?
        titlebar->setMinimumSize(Wt::WLength::Auto, "1.3em");
        titlebar->setMaximumSize(Wt::WLength::Auto, "1.3em");
        //titlebar->setMargin(0, Wt::Side::Bottom);
        //titlebar->setPadding(0, Wt::Side::Bottom);
        //titlebar->setMaximumSize();
        
        message_window->setText("Hello");
        message_window->setMinimumSize(Wt::WLength::Auto, 5);
        message_window->setMaximumSize(Wt::WLength::Auto, "50%");
    //     message_window = new Wt::WText("Hello <i>dear</i> visitor.This <b>is</b> the <p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p>", Wt::TextFormat::XHTMLText);
    //     message_window->setMinimumSize(Wt::WLength::Auto, 1);
        message_window->setStyleClass("message_window");
        Wt::WApplication::instance()->styleSheet().addRule(".message_window",
                                                        "background-color: white;"
                                                        "color:black;"
                                                        "display:block;"
                                                        "overflow:scroll;"
    //                                                        "overflow:hidden;"
                                                        "border: thin ridge black;");
        


        
        Wt::WVBoxLayout* horizontal_splitter = new Wt::WVBoxLayout;
        
        //BEGIN commented for testings
        horizontal_splitter->addWidget(titlebar, 0);
        horizontal_splitter->addWidget(message_window, 2/*, Wt::AlignmentFlag::AlignTop*/);
        //END commented for testing
        
    //     std::cout << horizontal_splitter->itemAt(0)->widget()->heighst().toPixels() << std::endl;
        
        
        Wt::WApplication::instance()->styleSheet().addRule(".left_top_box",
                                                        "background-color: orange;"
                                                        // // // // //                                                        "margin:0;"
                                                        // // // // //                                                        "border:0;"
                                                        "padding:20px;"
                                                        // // // // //                                                        "position:absolute;"
                                                        // // // // //                                                        "padding:0;"
                                                        // // // // //                                                        "margin:0;"
                                                        // // // // //                                                        "top:0;"
                                                        // // // // //                                                        "bottom:0;"
                                                        // // // // //                                                        "left:0;"
                                                        // // // // //                                                        "right:0;"
                                                        // // // // //                                                        "height:200px;"
                                                        // // // // //                                                        "margin-left:0px;"
                                                        // // // //                                                        "min-height:3em;"
                                                        //                                                        "max-height:600px;"
                                                        "display:block;"
                                                        "overflow:hidden;");
        // // // // //                                                        "border: thin ridge black;"
        // // // //         
        // // // //         left_top_box->setLayout(horizontal_splitter);
        
        
    //     setMinimumSize(Wt::WLength::Auto, 5);
    //     // setMaximumSize(Wt::WLength::Auto, left_bottom_box->height());
    //     // setMaximumSize(Wt::WLength::Auto, 600);
    //     // setMargin(100,Wt::Side::Left);
    // //     setOverflow(Wt::WContainerWidget::Overflow::OverflowHidden);
    //     setStyleClass("left_top_box");
        setSpacing(30);
    //     setLayout(horizontal_splitter);
        addLayout(horizontal_splitter);
        
        std::cout << "++++++++++++LeftTopBox is contstructed: " << std::endl;
    }

};



 
// 
// //happens in a thread
// // void MessagingArea::doSomethingM()
// // {
// //     //     std::cout << "++++++++++++void MessagingArea::doSomethingM(): " << std::endl;
// //     left_top_box->doSomethingL();
// // }
// 

 

// MessagingArea::MessagingArea(Wt::WContainerWidget *parent=0)
// This "HorizontalSplitter" has two BoxLayouts (which are the window areas).
MessageWindow::MessageWindow()
{

    //BEGIN sys msgs
    Wt::WText* system_messages_titlebar= new Wt::WText("&nbsp;System Messages", Wt::TextFormat::XHTMLText);
    system_messages_titlebar = new Wt::WText("&nbsp;System Messages", Wt::TextFormat::XHTMLText);
    system_messages_titlebar->height();
    system_messages_titlebar->setStyleClass("systemmessageviewer_titlebar");
    system_messages_titlebar->resize(Wt::WLength::Auto, "1.3em");
    system_messages_titlebar->setMinimumSize(Wt::WLength::Auto, "1.3em");
    system_messages_titlebar->setMaximumSize(Wt::WLength::Auto, "1.3em");
    //system_messages_titlebar->setPadding(0);
    //system_messages_titlebar->setPadding(6, Wt::Side::Left);
    Wt::WApplication::instance()->styleSheet().addRule(".systemmessageviewer_titlebar",
                                                       "color: white;"
                                                       "font-size:larger;"
                                                       "border: thin ridge black;"
//                                                     "margin:0;"
//                                                     "margin:0px;"
                                                       "display:block;"
                                                       "overflow:hidden;"
                                                       "background: linear-gradient(to bottom, silver, gray, dimgray);");
    
//         Wt::WText *system_messages_window = new Wt::WText("system messages go here.");
        Wt::WText *system_messages_window = new Wt::WText("system messages go here.  <b>is</b> the <p>message</p><p>message</p><p>message</p><p>message</p><p>message</p>");
//     system_messages_window->setMinimumSize(Wt::WLength::Auto, 200);
//     system_messages_window->resize(Wt::WLength::Auto, 400);
//     system_messages_window->setMargin(0);
//     Wt::WText* system_messages_window = new Wt::WText("system messages go here.", Wt::TextFormat::XHTMLText);
    system_messages_window->setStyleClass("system_messages_window");
    Wt::WApplication::instance()->styleSheet().addRule(".system_messages_window",
                                                       "color: white;"
                                                       "background-color: black;"
//                                                        "width:1400px;"
//                                                        "padding:0;"
//                                                        "position:absolute;"
//                                                                       "height:1;"
//                                                        "left:0;"
//                                                        "right:0;"
//                                                        "top:0;"      
//                                                        "bottom:0;"
//                                                        "height:100%;"
//                                                        "min-height:40px;"
//                                                        "margin-left:0px;"
//                                                         "min-height:0;"
                                                       "display:block;"
                                                       "overflow:auto;"
                                                       "border: thin ridge black;");
    
    //END sys msgs
    // addWidget(status_messages, 1);
    
//     setLayoutHint();
//     addWidget(titlebar, 1, Wt::AlignmentFlag::AlignTop);
//     addWidget(message_window, 1, Wt::AlignmentFlag::AlignTop);
//     addWidget(system_messages_titlebar, 1, Wt::AlignmentFlag::AlignTop);
//     addWidget(system_messages_window,1, Wt::AlignmentFlag::AlignTop);

    
    Wt::WVBoxLayout* left_top_window_box = new Wt::WVBoxLayout;
    //BEGIN commented for testing
//     left_top_window_box->addWidget(titlebar,1);
//     left_top_window_box->addWidget(message_window,100/*, Wt::AlignmentFlag::AlignTop*/);
    //END commented for testing
        
    
// // // //         Wt::WContainerWidget* left_top_box = new Wt::WContainerWidget;
//     left_bottom_box->setMinimumSize(Wt::WLength::Auto, "20px");
//         left_bottom_box->setMaximumSize(Wt::WLength::Auto, left_bottom_box->height());
//         left_bottom_box->setMaximumSize(Wt::WLength::Auto, 600);
//     left_bottom_box->setMargin(100,Wt::Side::Left);
//     left_bottom_box->setOverflow(Wt::WContainerWidget::Overflow::OverflowHidden);
// // // //     left_top_box->setStyleClass("left_top_box");
// // // //     Wt::WApplication::instance()->styleSheet().addRule(".left_top_box",
// // // //                                                        "background-color: orange;"
// // // // //                                                        "margin:0;"
// // // // //                                                        "border:0;"
// // // // //                                                        "padding:0;"
// // // // //                                                        "position:absolute;"
// // // // //                                                        "padding:0;"
// // // // //                                                        "margin:0;"
// // // // //                                                        "top:0;"
// // // // //                                                        "bottom:0;"
// // // // //                                                        "left:0;"
// // // // //                                                        "right:0;"
// // // // //                                                        "height:200px;"
// // // // //                                                        "margin-left:0px;"
// // // //                                                        "min-height:3em;"
// // // // //                                                        "max-height:600px;"
// // // //                                                        "display:block;"
// // // //                                                        "overflow:hidden;");
// // // // //                                                        "border: thin ridge black;"
// // // //         
// // // //         left_top_box->setLayout(left_top_window_box);

        
        
        
        
    
    
    

//         Wt::WContainerWidget* left_top_window_box_container = new Wt::WContainerWidget;
//         left_top_window_box_container->setLayout(left_top_window_box);;
    
    
    Wt::WVBoxLayout* left_bottom_window_box_splitter = new Wt::WVBoxLayout;
//     left_bottom_window_box_splitter->setMinimumSize
//     left_bottom_window_box_splitter->setSpacing(0);
//     left_bottom_window_box_splitter->setContentsMargins(10,10,10,10); //set all the 0 after debugging.
    //BEGIN
    left_bottom_window_box_splitter->addWidget(system_messages_titlebar,1);
    left_bottom_window_box_splitter->addWidget(system_messages_window,100);
    //END
    
    Wt::WContainerWidget* left_bottom_box = new Wt::WContainerWidget;
//     left_bottom_box->setMinimumSize(Wt::WLength::Auto, "0px");
//         left_bottom_box->setMaximumSize(Wt::WLength::Auto, "90%");
//         left_bottom_box->setMaximumSize(Wt::WLength::Auto, 600);
//     left_bottom_box->setMargin(100,Wt::Side::Left);
//     left_bottom_box->setOverflow(Wt::WContainerWidget::Overflow::OverflowHidden);
    left_bottom_box->setStyleClass("left_bottom_box");
    Wt::WApplication::instance()->styleSheet().addRule(".left_bottom_box",
                                                       "background-color: yellow;"
//                                                        "margin:0;"
//                                                        "border:0;"
//                                                        "padding:0;"
//                                                        "padding:0;"
//                                                        "margin:0;"
// //                                                        "position:absolute;"
// //                                                        "top:50%;"
// //                                                        "bottom:0;"
// //                                                        "min-height:300px;"
// //                                                        "max-height:200px;"
// //                                                        "min-height:0;"
//                                                        "left:0;"
//                                                        "right:0;"
//                                                        "height:100%;"
//                                                        "widght:100%;"
//                                                        "height:200px;"
//                                                        "margin-left:0px;"
//                                                        "min-height:200px;"
//                                                        "max-height:500px;"
//                                                        "height:500px;"
                                                    "min-height:3em;"
//                                                        "display:block;"
//                                                        "min-height:200px;"
//                                                        "max-height:200px;"
//                                                        "overflow:hidden;"
//                                                        "border: thin ridge black;"
                                                       
                                                       "display:block;"
                                                       "overflow:hidden;"
                                                      );
    left_bottom_box->setLayout(left_bottom_window_box_splitter);
//     left_bottom_box->setLayout(left_bottom_window_box_splitter);
    
//     left_bottom_box->setMinimumSize(1, 1);
//     left_bottom_box->setMaximumSize(100, 100);
//     left_bottom_box->resize("100%", "500pxs");
//     left_bottom_box->setOverflow(Wt::WContainerWidget::Overflow::OverflowHidden);
//     addLayout(left_bottom_window_box_splitter/*,0, Wt::AlignmentFlag::AlignBottom*/);

    //addWidget(new LeftTopBox,0);
    addLayout(new LeftTopBox, 1);
    //addWidget(left_bottom_box,0);//, Wt::AlignmentFlag::AlignJustify);
//     left_top_box = new LeftTopBox;
//     addWidget(left_top_box,0);
    addLayout(new LeftTopBox, 2);
//         addWidget(titlebar, 1);
//     addWidget(message_window, 1);
    
//     addWidget(system_messages_titlebar,1);
//     addWidget(system_messages_window,2);
    
    setContentsMargins(20,20,20,20);
    setSpacing(4); //2 looks great
//     addWidget(system_messages_titlebar);
//     addWidget(system_messages, 1);//, Wt::AlignmentFlag::AlignBottom | Wt::AlignmentFlag::AlignJustify);
    setResizable(0);
    
//     left_bottom_box->setHeight("100%");
//     update();
}



/*
 * GUI splits the screen vertically. GUI is a horizontal layout manager derived from Wt::WHBoxLayout.
 * GUI is added directly on to the web application, as apposed to to a Wt::WContainerWidget (because it seems 
 * to be doing the trick).
 * 
 * On the right side of GUI is <right side layout>.
 * 
 * On the left side is the LeftSideBorder, which is visible. Within that is another border (div) which won't
 * be visible. That one is to maintain a border around the text items. Then within that is a horizontal splitter, which
 * contains the two dialog boxes (with title and text). tbc jwicks Sunday, January 12, 2014
 * 
 * rendered directly on the web application. vertical splitter 
 * a Wt::WContainerWidget (called border or something).
 * two subclasses: GUI & System messages (both derive from base class but set their own color and whatever else differs
 * a horizontal splitter (where items are arranged vertically) (call it ViewSplitter)
 */

/*      
 * As far as I can tell, layout items can't be decorated. Therefore, the left side's outermost container
 * is a Wt::WContainerWidget because it is an aesthetic element - It provides the border that holds the two 
 * viewers (GUI & System messages).
 * 
 * The space within the border is split horizontally, into top and bottom, with a vertical layout manager
 * (Wt::WVBoxLayout (MessagingArea rename to vertical splitter or something)). (gui messages and system messages
 * respectively). 
 * 
 * System and GUI message windows/areas are     made of two rectangles: title bar and text output area. (WText 
 * and WText). The text output area is a WScrollArea (to add better scrollbar handling than css (citation),
 * which, finally, contains a WText widget.
 */
// 
// 
// 
// 
// GUI::GUI(Wt::WContainerWidget *parent=0)
// :
//     Wt::WHBoxLayout(parent)   
// {
//     
//     left_side_border->setStyleClass("silverborder");
//     MessagingArea* me = new MessagingArea;
//     left_side_border->setLayout(me); //adding an item (a layout item).
// 
//     
// 
//     
//     addWidget(left_side_border, 1);
//     
//     setContentsMargins(50, 50, 50, 50); // Add space around all four sides. Too bad I can't specify in %.
//     setSpacing(50); 
// }
// 